package com.example.mobile2032021606170hsy0602homework;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainName;
    private Button btnMainLogin;
    private Button btnMainReset;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sp = getSharedPreferences("login", MODE_PRIVATE);
        chkBoxMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkName", isChecked);
                editor.commit();
            }
        });
        chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkPSW",isChecked);
                editor.commit();
                if (isChecked) {
                    chkBoxMainName.setChecked(true);
                    chkBoxMainName.setEnabled(false);
                } else {
                    chkBoxMainName.setEnabled(true);
                }
            }
        });
        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = edtTxtMainUserName.getText().toString().trim();
                String password = edtTxtMainPassword.getText().toString().trim();
                if ((userName.equals("admin") && password.equals("admin"))) {
                    if (chkBoxMainName.isChecked()) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("userName", userName);
                        editor.commit();
                    }
                    Intent intent = new Intent(MainActivity.this, ShowDataActivity.class);
                    intent.putExtra("passWord", edtTxtMainPassword.getText().toString().trim());
                    Bundle bundle = new Bundle();
                    bundle.putString("passWord", edtTxtMainPassword.getText().toString().trim());
                    startActivity(intent);
                }else {
                    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage("请输入的用户名或密码错误！");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            MainActivity.this.finish();
                        }
                    });
                    AlertDialog dialog =builder.create();
                    dialog.show();
                    if (chkBoxMainPSW.isChecked()) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("password", password);
                        editor.commit();
                    }
                }
            }
        });
    }

    protected void onStart() {
        super.onStart();
        boolean chkName = sp.getBoolean("chkName", false);
        chkBoxMainName.setChecked(chkName);
        boolean chkPSW = sp.getBoolean("chkPSW", false);
        chkBoxMainPSW.setChecked(chkPSW);
        if (chkName) {
            edtTxtMainUserName.setText(sp.getString("userName", null));
        }
        if (chkPSW) {
            edtTxtMainPassword.setText(sp.getString("password", null));
        }
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        chkBoxMainName = (CheckBox) findViewById(R.id.chkBoxMainName);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
    }
}